package com.collection.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.collection.dao.PublishConfigDao;
import com.collection.entity.PublishConfig;
import com.collection.service.PublishConfigService;

@Service
public class PublishConfigServiceImpl implements PublishConfigService {

	@Autowired
	private PublishConfigDao publishConfigDao;
	
	/*
	 * (non-Javadoc)
	 * @see com.collection.service.PublishConfigService#createPublishConfig(com.collection.entity.PublishConfig)
	 */
	@Override
	public void createPublishConfig(PublishConfig publishConfig) {
		if(publishConfig != null) {
			publishConfigDao.save(publishConfig);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.collection.service.PublishConfigService#deletePublishConfig(java.lang.Integer)
	 */
	@Override
	public void deletePublishConfig(Integer id) {
		if(id != null) {
			PublishConfig publishConfig = getPublishConfigById(id);
			if(publishConfig != null) {
				deletePublishConfig(publishConfig);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.collection.service.PublishConfigService#deletePublishConfig(com.collection.entity.PublishConfig)
	 */
	@Override
	public void deletePublishConfig(PublishConfig publishConfig) {
		if(publishConfig != null) {
			publishConfigDao.delete(publishConfig);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.collection.service.PublishConfigService#getPublishConfigById(java.lang.Integer)
	 */
	@Override
	public PublishConfig getPublishConfigById(Integer id) {
		if(id != null) {
			return publishConfigDao.get(PublishConfig.class, id);
		}
		return null;
	}

	public PublishConfig getPublishConfigByTaskId(Integer taskId) {
		if(taskId != null) {
			List<PublishConfig> publishConfigs = publishConfigDao.queryByHQL("from PublishConfig pc where pc.collectionTask.id=?", new Object[]{taskId});
			if(publishConfigs != null && publishConfigs.size()>0) {
				return publishConfigs.get(0);
			}
		}
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.collection.service.PublishConfigService#modifyPublishConfig(com.collection.entity.PublishConfig)
	 */
	@Override
	public void modifyPublishConfig(PublishConfig publishConfig) {
		if(publishConfig != null) {
			publishConfigDao.update(publishConfig);
		}
	}

}
